<template>
  <div v-if="visible">
    <el-dialog :visible.sync="visible"
               :title="!dataForm.id ? $t('add') : $t('update')"
               :modal="false"
               class="user-dialog add-or-updata"
               width="35%"
               :before-close="closeDialog"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form :model="dataForm"
               class="label-left-form"
               v-loading="dataListLoading"
               :element-loading-text="$t('loading')"
               ref="dataForm"
               label-position="left"
               @keyup.enter.native="dataFormSubmitHandle()"
               label-width="80px">
        <el-form-item prop="type"
                      label="类型"
                      :rules="required()">
          <el-select v-model="dataForm.type"
                     placeholder="请选择类型"
                     class="width-100">
            <el-option v-for="item in typeList"
                       :key="item.dictValue"
                       :label="item.dictName"
                       :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="label"
                      label="标签"
                      :rules="stringRule(false,1,50)">
          <el-input v-model.trim="dataForm.label"
                    placeholder="支持多个 ; 分号隔开"></el-input>
        </el-form-item>
        <el-form-item prop="sort"
                      label="排序">
          <el-input-number v-model.trim="dataForm.sort"
                           controls-position="right"
                           :min="0"
                           :max="99999999"
                           class="width-100"
                           :label="$t('dict.sort')"></el-input-number>
        </el-form-item>
        <el-form-item label="法律文件"
                      :rules="required()"
                      prop="fileId">
          <el-select v-model="dataForm.fileId"
                     class="width-100"
                     placeholder="请选择法律文件">
            <el-option v-for="item in legalOptions"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="content"
                      :rules="required()"
                      label="法律内容">
          <tinymce-light ref="tinymce"
                         :wordLimit='500'
                         :height='290' />
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button @click="closeDialog()">{{ $t('cancel')}}</el-button>
        <el-button type="primary"
                   :loading="dataListLoading"
                   @click="dataFormSubmitHandle()">{{ $t('confirm')}}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
import tinymceLight from '@/components/tinymce-light'
import debounce from 'lodash/debounce'
import { mapState } from 'vuex'
export default {
  mixins: [mixinViewModule, minFormValidate],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '', // 数据列表接口，API地址
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        addURL: '/tb/law/addLaw', // 添加或修改接口，API地址
        updateURL: '/tb/law/updateLaw', // 添加或修改接口，API地址
        getDataInfoURL: '/tb/law/getLaw?id=', // 数据详情，API地址
        deleteURL: '', // 删除接口，API地址
        deleteIsBatch: true, // 删除接口，是否需要批量？
        exportURL: '' // 导出接口，API地址
      },
      legalOptions: [],
      visible: false,
      dataForm: {
        type: '',
        label: '',
        sort: 0,
        content: '',
        fileId: '',
        constent: ''
      }
    }
  },
  computed: {
    ...mapState({
      typeList: state => state.dict.typeList
    })
  },
  components: {
    tinymceLight
  },
  methods: {
    closeDialog () {
      this.visible = false
      this.$refs.dataForm.resetFields()
    },
    init (data) {
      data = data || {}
      this.dataForm.id = data.id
      this.legalOptions.splice(0, this.legalOptions.length, ...data.list)
      this.visible = true
      if (data.id) {
        this.getDataInfo().then(res => {
          if (res.code !== 0) {
            return
          }
          this.$refs.tinymce.init(res.data.content)
        })
      }
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.dataForm.content = this.$refs.tinymce.getContent()
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          if (!this.$refs.tinymce.getVerification()) {
            return this.$message.warning('字数不能超过200字')
          }
          // this.dataForm.content = content
          const url = this.dataForm.id ? this.mixinViewModuleOptions.updateURL : this.mixinViewModuleOptions.addURL
          this.addOrUpdateItem({ url }).then(res => {
            if (res.code !== 0) {
              return
            }
            this.$emit('callBack')
            this.closeDialog()
          })
        }
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>

<style lang="scss" scoped>
</style>
